var common = require('../../common.js');
var app = getApp();
Page({
  data: {
    userinfo:{},
    detail:{}
  },
  onLoad: function (options) {
    wx.request({
      url: "https://kk.timemost.com/api/index/inspire",
      method: 'POST',
      data: {
        token:common.token,
        openid: wx.getStorageSync('userInfo').openid,
        appid:wx.getAccountInfoSync().miniProgram.appId
      },
      success: res => {
          if(res.data.status == 1){
            this.setData({detail:res.data.data});
          }
      }
    });
  },
  toBack: function () {
    wx.navigateBack({
     delta: 1
    });
  },
  onShareAppMessage: function () {
    var appInfo = app.globalData.appInfo;
    return {
      title: appInfo.shareTitle,
      path: 'pages/index/index',
      imageUrl:  appInfo.shareIcon
    }
  },
  openAdsense:function(){
    var videoAd = null;
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: this.data.detail.adsense
      });
      videoAd.onClose((res) => {
        if (res && res.isEnded) {
          wx.request({
            url: "https://kk.timemost.com/api/index/doinspire",
            method: 'POST',
            data: {
              token:common.token,
              appid:wx.getAccountInfoSync().miniProgram.appId,
              openid:this.data.userinfo.openid
            },
            success: res => {
              wx.showToast({
                title: res.data.msg,
                icon : 'none'
              });
            }
          });
          
        } else {
           wx.showToast({
             title: '您取消了，未获得奖励',
             icon: 'none'
           });
        }
      });
    }
    if (videoAd) {
      videoAd.show().catch(() => {
        videoAd.load()
          .then(() => videoAd.show())
          .catch(err => {
          })
      })
    }
  }
})